package bdd.Otros;

import VIRTUAL.*;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class LOGIN2 extends javax.swing.JFrame {

    private Connection conexion;
    conexion con = new conexion();
    ResultSet rss;
    ResultSet rs;
    public static String inv = " ";
    public static String variable1;
    public static String a;
    public static String nombre;

    public LOGIN2() {
       //PARA AJUSTAR EL JFRAME EN EL CENTRO
        setLocationRelativeTo(null);
        this.setResizable(false);
        if(con.conectar()!=null){
                initComponents();
iconos();
        }else{   
         gen_admin();
       }

    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        user = new javax.swing.JTextField();
        pass = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/salir.jpg"))); // NOI18N
        jButton3.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 240, 90, 30));

        jButton5.setBackground(new java.awt.Color(255, 255, 255));
        jButton5.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
        jButton5.setForeground(new java.awt.Color(255, 255, 255));
        jButton5.setText("Registrarse");
        jButton5.setBorderPainted(false);
        jButton5.setContentAreaFilled(false);
        jButton5.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jButton5.setDefaultCapable(false);
        jButton5.setFocusPainted(false);
        jButton5.setFocusable(false);
        jButton5.setRequestFocusEnabled(false);
        jButton5.setRolloverEnabled(false);
        jButton5.setVerifyInputWhenFocusTarget(false);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 70, 120, 20));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ADMINISTRADOR", "INVITADO" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 190, 140, 30));

        jLabel8.setFont(new java.awt.Font("Trebuchet MS", 3, 18)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Contraseña:");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 150, -1, 29));

        jLabel9.setAutoscrolls(true);
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, 170, 160));

        jLabel2.setFont(new java.awt.Font("Trebuchet MS", 3, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Usuario:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 100, -1, -1));

        jLabel3.setFont(new java.awt.Font("Trebuchet MS", 3, 18)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Perfil:");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 190, -1, 29));

        user.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                userKeyPressed(evt);
            }
        });
        getContentPane().add(user, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 100, 189, 29));

        pass.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passKeyPressed(evt);
            }
        });
        getContentPane().add(pass, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 150, 190, 29));

        jLabel5.setFont(new java.awt.Font("Calisto MT", 1, 24)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("ACCESO AL SISTEMA ");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 20, -1, -1));

        jButton1.setText("ENTRAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 240, -1, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/fondo_Jframe.jpg"))); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 10, 510, 270));

        pack();
    }// </editor-fold>//GEN-END:initComponents
   public void gen_admin(){
          try {
                java.sql.Statement stmt = con.getConexion().createStatement();
                rs = stmt.executeQuery("select * from usuarios");
                if (rs.next()) {
                    login();
                } else {
                    CallableStatement prc = con.getConexion().prepareCall("{call login }");
                    prc.executeUpdate();
                    login();
                }
            } catch (Exception ex) {
             JOptionPane.showMessageDialog(null,ex.getMessage());
            }
   }
    public void iconos(){
              // poner icono en JFRAME
        Image icon = new ImageIcon(getClass().getResource("/imagenes/LOGO1.jpg")).getImage();
        setIconImage(icon);
          jLabel9.setIcon(ajustarImagen());
        jLabel1.setIcon(ajustarImagen2());
        
   }
    public void login() {
        initComponents();
                  //PARA AUJSTAR EL JFRAME EN EL CENTRO
        setLocationRelativeTo(null);
        // codigo para maximizar      this.setExtendedState(MAXIMIZED_BOTH);
        // poner icono en JFRAME
        Image icon = new ImageIcon(getClass().getResource("/imagenes/LOGO1.jpg")).getImage();
        setIconImage(icon);

        user.setBackground(Color.LIGHT_GRAY);
        user.setForeground(Color.WHITE);
        pass.setBackground(Color.LIGHT_GRAY);
        pass.setForeground(Color.WHITE);
        jLabel9.setIcon(ajustarImagen());
        jLabel1.setIcon(ajustarImagen2());
    }

    public ImageIcon ajustarImagen() {
        ImageIcon IconAux = new ImageIcon(getClass().getResource("/imagenes/admon2.png"));
        ImageIcon temp = new ImageIcon(IconAux.getImage().getScaledInstance(jLabel9.getWidth(), jLabel9.getHeight(), Image.SCALE_DEFAULT));
        return temp;
    }

    public ImageIcon ajustarImagen2() {
        ImageIcon IconAux = new ImageIcon(getClass().getResource("/imagenes/fondo_jframe.jpg"));
        ImageIcon temp = new ImageIcon(IconAux.getImage().getScaledInstance(jLabel1.getWidth(), jLabel1.getHeight(), Image.SCALE_DEFAULT));
        return temp;
    }
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed

    }//GEN-LAST:event_jButton5ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        if (jComboBox1.getSelectedItem().equals("ADMINISTRADOR")) {

            ImageIcon IconAux = new ImageIcon(getClass().getResource("/imagenes/admon2.png"));
            ImageIcon temp = new ImageIcon(IconAux.getImage().getScaledInstance(jLabel9.getWidth(), jLabel9.getHeight(), Image.SCALE_DEFAULT));
            jLabel9.setIcon(temp);
        } else {
            if (jComboBox1.getSelectedItem().equals("INVITADO")) {
                ImageIcon IconAux = new ImageIcon(getClass().getResource("/imagenes/inv1.png"));
                ImageIcon temp = new ImageIcon(IconAux.getImage().getScaledInstance(jLabel9.getWidth(), jLabel9.getHeight(), Image.SCALE_DEFAULT));
                jLabel9.setIcon(temp);
            }
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed
    public void entrar() {
        con.conectar();
        inv = user.getText();
        try {
            if (user.getText().trim().length() == 0 || pass.getText().trim().length() == 0) {
                JOptionPane.showMessageDialog(this, "Algun campo está vacío");
            } else {
                java.sql.Statement stmt = con.getConexion().createStatement();
                String cap = "", cap2 = "";
                rss = stmt.executeQuery("select * from usuario where nombre_usuario= '" + user.getText() + "' and CONTRASEÑA= '" + pass.getText() + "' and tipo_usuario='"+jComboBox1.getSelectedItem()+"'");
                while (rss.next()) {
                    cap = rss.getString("tipo_usuario");
                }

                if (cap.equals(jComboBox1.getSelectedItem())) {
                    PRINCIPAL ingreso = new PRINCIPAL();
                    ingreso.setVisible(true);
                    dispose();


                    //codigo el cual manda los datos capturardos de Jtext user los manda 
                    //jlabel recibe de label menu_admin
                    rs = stmt.executeQuery("select nombre from usuario where nombre_usuario= '" + user.getText() + "'");
                    if (rs.next()) {
                        ingreso.recibe.setText(cap = rs.getString("nombre"));
                    }
                }else{
                    JOptionPane.showMessageDialog(null,"Sus datos son incorrectos");
                }
                //        if((!cap.equals(jComboBox1.getSelectedItem()))&& (!cap2.equals(jComboBox1.getSelectedItem())))
                //  {
                //       JOptionPane.showMessageDialog(this, "No existe sus datos");
                //   } 

            }
        } catch (Exception ex) {
    JOptionPane.showMessageDialog(null,"No logró conectarse con el servidor");
        }
    }
    private void passKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            entrar();
        }
    }//GEN-LAST:event_passKeyPressed

    private void userKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_userKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            entrar();
        }
    }//GEN-LAST:event_userKeyPressed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    entrar();
}//GEN-LAST:event_jButton1ActionPerformed

    public static void main(String args[]) {
   java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LOGIN2().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPasswordField pass;
    public static javax.swing.JTextField user;
    // End of variables declaration//GEN-END:variables
}
